#include "NetworkEvents.h"

//Writes the event to a bitstream and sends it over the network
void ClientEvent::send()
{
	//Get the packet identifier from the subclass
	int iPacketID = this->getPacketID();
	//Check for failed packetID generation
	if ( -1 == iPacketID ) {
		std::cerr << "Error in ClientEvent::send(): Cannot send event because \
packetID generation failed."
		          << std::endl;
		return;
	}
	
	//Create a bitstream to hold the event
    RakNet::BitStream bsEvent;
    //Write the packet identifier
    bsEvent.Write( (unsigned char)iPacketID );
    //Get the subclass to encode itself into the bitstream
	this->encode( bsEvent );
    //Send the bitstream over the network to the server, using the previously-
    // set priority and reliability
	Multiplayer::get()->getClient()->Send(&bsEvent, m_iPriority,
	                                                         m_iReliability, 0);
}

//------------------------------------------------------------------------------

//Writes the event to a bitstream and sends it over the network
void ServerEvent::send( PlayerID pidPlayer )
{
	//Get the packet identifier from the subclass
	int iPacketID = this->getPacketID();
	//Check for failed packetID generation
	if ( -1 == iPacketID ) {
		std::cerr << "Error in ServerEvent::send(): Cannot send event because \
packetID generation failed."
		          << std::endl;
		return;
	}
	
	//This makes it send only to that player specified if one is specified
    bool bBroadcast = (pidPlayer == UNASSIGNED_PLAYER_ID);
    
    //Create a bitstream to hold the event
    RakNet::BitStream bsEvent;
    //Write the packet identifier
    bsEvent.Write( (unsigned char)iPacketID );
    //Get the subclass to encode itself into the bitstream
	this->encode( bsEvent );
	
	//Send the bitstream over the network to all clients, using the previously-
	// set priority and reliability
    Multiplayer::get()->getServer()->Send(&bsEvent, m_iPriority,
	                                  m_iReliability, 0, pidPlayer, bBroadcast);
}
